/*
 * TryHTMLToXML.scala
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package trypkg
import scala.xml._
import scala.xml.dtd._
import org.xml.sax.InputSource
import scala.io._
import java.util.regex._

object TryHTMLToXML {
    def main(args : Array[String]) = {
        object ConsoleWriter extends java.io.Writer{
            def close() = {}
            def flush() = Console.flush
            def write(cbuf : Array[char], off : int, len : int) : Unit = {
                var i = 0
                while(i < len)
                Console.print(cbuf(off + i))
            }
        }

        val strUrl = "http://club.stock.sohu.com/r-stock-10701074-0-0-900.html"
        val in = new java.io.InputStreamReader(new java.net.URL(strUrl).openStream)
        var input = new java.lang.StringBuilder
        var c = in.read
        while(c != -1){
            input.append(c.asInstanceOf[Char])
            //println("c= " + c)
            c = in.read
        }
        val end = input.indexOf("\">")
        input.delete(0, end+3)
        var strInput = styleFilter(input.toString)
        strInput = scriptFilter(strInput.trim)
        try{
            var myXml = XML.loadString(strInput.trim)
            val docType = DocType("html", PublicID("-//W3C//DTD XHTML 1.1//EN","../default.dtd"), Nil)

            XML.write(ConsoleWriter, myXml, "utf-8", true, docType)
        }catch{
            case e : Exception =>
                println(e.toString)
        }
    }

    def scriptFilter(oldStrInput : java.lang.String) : String = {
        println("script ooxx stated")
        var strInput = oldStrInput
        try{
            val scriptPattern = "(?is)<script(.+?)</script>"
            val scrPattern = Pattern.compile(scriptPattern)
            var scrMatcher = scrPattern.matcher(strInput)
            strInput = scrMatcher.replaceAll("")
            println("vivizhyy ooxx script")
        }catch{
            case e : Exception =>{
                    println("")
                    println(e.toString)
                }

        }
        strInput
    }

    def styleFilter(oldStrInput : java.lang.String) : String = {
        println("style ooxx stared")
        var strInput = oldStrInput
        try{
            val StylePattern = "(?is)<style(.+?)</style>"
            val stPattern = Pattern.compile(StylePattern)
            var stMatcher = stPattern.matcher(strInput)
            strInput = stMatcher.replaceAll("")
            println("vivizhyy ooxx styles")

        }catch{

            case e : Exception =>{
                    println("style exception")
                    println(e.toString)
                }
        }
        strInput
    }
}
